package cn.wzl.recognition.mvc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Controller implements ActionListener {

	private final Model model;

	public Controller(Model model) {
		this.model = model;
		model.getView().getClearButton().addActionListener(this);
		model.getView().getRunButton().addActionListener(this);
		model.getView().getTrainingButton().addActionListener(this);
		model.getView().getTrainThisPicButton().addActionListener(this);

		model.getView().addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                model.mousePressed(e);
            }
    	});
		model.getView().addMouseMotionListener(new MouseAdapter(){
    		public void mouseDragged(MouseEvent e){
				model.mouseDragged(e);
            }  
    	});
	}

	public void actionPerformed(ActionEvent e) {
    	if(e.getSource() == model.getView().getClearButton()) {
			model.clearPanel();
    	}
    	else if(e.getSource() == model.getView().getRunButton()) {
			model.run();
    	}
    	else if(e.getSource() == model.getView().getTrainingButton()) {
    		model.getView().getTrainingButton().setEnabled(false);
    		int times = Integer.parseInt(model.getView().getTrainingTimes().getText());
    		model.training(times);
			model.getView().getTrainingButton().setEnabled(true);
		}
    	else if(e.getSource() == model.getView().getTrainThisPicButton()) {
			int expectedValue = Integer.parseInt(model.getView().getExpectedValue().getText());
			int times = Integer.parseInt(model.getView().getTrainingTimes().getText());
			model.getView().getTrainThisPicButton().setEnabled(false);
			model.trainingCurrentPic(expectedValue, times);
			model.getView().getTrainThisPicButton().setEnabled(true);

		}
	}
}
